package com.itheima.reggie.contorller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page(page, pageSize);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        dishService.page(dishPage, lambdaQueryWrapper);

        Page<DishDto> dishDtoPage = new Page<>();

        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<Dish> records = dishPage.getRecords();

        List<DishDto> records1 = new ArrayList<>();

        for (Dish record : records) {
            Long categoryId = record.getCategoryId();

            Category byId = categoryService.getById(categoryId);

            String name1 = byId.getName();

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record, dishDto);

            dishDto.setCategoryName(name1);

            records1.add(dishDto);
        }
        dishDtoPage.setRecords(records1);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R<String> dishAdd(@RequestBody DishDto dishdto) {
        dishService.addDandF(dishdto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id) {
        Dish byId = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId, dishDto);
        Long categoryId = byId.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String name = category.getName();
        dishDto.setCategoryName(name);
        Long id1 = byId.getId();
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id1);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> updateById(@RequestBody DishDto dishDto) {
        dishService.updateById(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long id1 = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(DishFlavor::getDishId, id1);
        dishFlavorService.remove(lambdaQueryWrapper1);
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id1);
        }
        dishFlavorService.saveBatch(flavors);
        String categoryName = dishDto.getCategoryName();
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getName, categoryName);
        Category one = categoryService.getOne(lambdaQueryWrapper);
        Long id = one.getId();
        dishDto.setCategoryId(id);
        dishDto.setCategoryName(categoryName);
        return R.success("更新成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        //构建查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //加条件
        wrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        // 套餐里面加停售的还是启售的   启售的  status 是1 的
        wrapper.eq(Dish::getStatus, 1);
        //你想排就排  --- 有排序字段  没有就按照 更新日期倒序
        wrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(wrapper);

        // 返回 List<DishDto>
        //  把 每一个dish---DishDto  dishDto 根据 菜品id查询到 对应的 口味信息
        //封装到 sishDto的 flavors  属性中
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish1 : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1, dishDto);
            // 根据拿到的 dishid 去查询相关的  口味信息
            Long dishId = dish1.getId();
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> flavors = dishFlavorService.list(wrapper1);
            dishDto.setFlavors(flavors);
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        }
        return R.success(dishDtoList);
    }

    @DeleteMapping
    public R<String> deleteTwo(@RequestParam List<Long> ids) {
        dishService.removeTwo(ids);
        return R.success("删除成功");
    }
}
